import React from 'react'
// ===================================================================== Temp
import FormModal from '#cpt/temp/FormModal'
// ===================================================================== global declare
const { $fn, $Lazy, $http } = window
// ===================================================================== $Lazy
const $cache 			= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data: [],
		// 条码作废
		formData:[
			{ label: '被授权部门',	name: 'depart_id',	type: 'select', 	full:true,	nameStr:'depart_name',	idStr:'uuid',
				onValue:({value,formData}) => {
					$http.pull( null, 'employee/select', {param:{depart_id:value}} ).then((data) => {
						$fn.setRow(formData, 'perm_users', data)
						this.setState({ formData })
					})
				}
			},
			{ label: '被授权人',		name: 'perm_users',	type: 'select', 	full:true,	mode:'multiple', },
			{ label: '权限',		name: 'perm_codes', type: 'checkboxGroup',	full:true,	required:true,	idStr:'perm_code',nameStr:'title' },
		],
		api:''
	}
	submit = {}
	open = (row,boolean)=> {
		const api = boolean?'doc-file-perm/edit':'doc-file-perm/add'
		this.submit.uuids =row.map(v=>v.uuid)
		this.getModalSelect()
		const { perm_codes, share_user_id } = row[0]
		if($fn.hasArray(perm_codes)&&$fn.hasArray(share_user_id)){
			this.formRef.setValue({'perm_users': share_user_id,'perm_codes':perm_codes})
		}
		this.setState({api})
		this.modal.open({formRef: this.formRef})
	}
	
	// 弹窗获取下拉数据
	getModalSelect = async () => {
		const { formData } = this.state
		$cache.then( async f => {
			// 部门
			$fn.setRow(formData, 'depart_id', await f.getCache({ api:'department' } ) )
			$fn.setRow( formData, 'perm_users', await f.getCache({ api:'employee' }) )
			$fn.setRow( formData, 'perm_codes', await f.getCache({ api:'doc-file-perm/permSelect' }) )
			this.setState({ formData })
		})
	}
	
	render(){
		return FormModal.call(this, {title:'权限设置', labelWidth:'s6', width:700, api:this.state.api,
			onRefresh:()=> this.props.onRefresh()
		} )
	}
}

export default Index